package servlet;

import com.google.gson.Gson;
import dao.DB;
import service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;


//查寻好友的信息
@WebServlet("/pageFriinfo")
public class SelepageFriendinfo extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        String friacc = req.getParameter("acc");

        Cookie[] cookies = req.getCookies();
        String newToken = null;
        for (Cookie cookie : cookies) {
            if (cookie.getName().equals("TOKEN")) {
                newToken = cookie.getValue();
            }
        }

        String uid =  DB.executeQuery("SELECT account FROM `user` WHERE token = ?" ,newToken).get(0).get("account").toString();
        UserService userSer =new UserService();
        List fri = userSer.getPagefriinfo(uid,friacc);
        resp.getWriter().print(new Gson().toJson(fri));



    }
}
